package com.security.demo.alipay.page;

import com.security.core.properties.AliPayProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * AliPayController
 *
 * @author: Dark_Blue
 * @date: 2020/3/13 18:19
 */
@RestController
@RequestMapping("/aliPay/page")
public class AliPayPageController {

    @Autowired
    private AliPayPageService aliPayPageService;

    @Autowired
    private AliPayProperties aliPayProperties;

    /**
     * 创建订单
     *
     * @param orderNo     订单号（系统中唯一订单号）
     * @param orderName   订单名称
     * @param amount      订单金额
     * @param description 订单描述
     * @return
     */
    @GetMapping("/order")
    public void createOrder(@RequestParam String orderNo, @RequestParam String orderName, @RequestParam String amount, @RequestParam String description, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=" + aliPayProperties.getCharset());
            response.getWriter().write(aliPayPageService.createOrder(orderNo, orderName, amount, description));//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        return "ok";
    }

    /**
     * 获取订单信息
     * 选择二选一设置
     *
     * @param orderNo 订单号
     * @param tradeNo 支付宝交易号
     * @return
     */
    @GetMapping("/orderInfo")
    public String getOrder(@RequestParam String orderNo, @RequestParam String tradeNo, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=" + aliPayProperties.getCharset());
            response.getWriter().write(aliPayPageService.getOrderInfo(orderNo, tradeNo));//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }

    /**
     * 退款
     *
     * @param orderNo         商户订单号
     * @param tradeNo         支付宝交易号
     * @param amount          退款金额
     * @param reason          退款原因
     * @param refundRequestNo 退款请求号
     * @return
     */
    @GetMapping("/refund")
    public String orderRefund(@RequestParam String orderNo, @RequestParam String tradeNo, @RequestParam String amount, @RequestParam String reason, @RequestParam String refundRequestNo, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=" + aliPayProperties.getCharset());
            response.getWriter().write(aliPayPageService.refund(orderNo, tradeNo, amount, reason, refundRequestNo));//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }

    /**
     * 退款查询
     *
     * @param orderNo         商户订单号
     * @param tradeNo         支付宝交易号
     * @param refundRequestNo 退款请求号
     * @return
     */
    @GetMapping("/refundInfo")
    public String refundQuery(@RequestParam String orderNo, @RequestParam String tradeNo, @RequestParam String refundRequestNo, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=" + aliPayProperties.getCharset());
            response.getWriter().write(aliPayPageService.refundQuery(orderNo, tradeNo, refundRequestNo));//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }

    /**
     * 交易关闭
     * 选择二选一设置
     *
     * @param orderNo 订单号
     * @param tradeNo 支付宝交易号
     * @return
     */
    @GetMapping("/close")
    public String close(@RequestParam String orderNo, @RequestParam String tradeNo, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=" + aliPayProperties.getCharset());
            response.getWriter().write(aliPayPageService.close(orderNo, tradeNo));//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }
}
